package controller

import (
	"github.com/doujunyu/gogo/gogo_log"
	"github.com/doujunyu/gogo/job"
	"go_self/server"
)

// CategoryByList 种类列表
func CategoryByList(j *job.Job) {
	data := server.CategoryByList(j.Input)
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// GoodsList 首页商品
func GoodsList(j *job.Job) {
	//isLogin,err := server.GetToken(j.GroupData["token"].(string))
	//if err != nil {
	//
	//}

	input := make(map[string]string)
	input["orderBy"] = "is_recommend desc, is_hot desc, click_count desc,id desc"
	input["up_down"] = "1"
	input["status"] = "1"
	input["page"] = j.Input["page"]
	input["page_size"] = j.Input["page_size"]
	data := server.GoodsByList(input)
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

func GoodsSearch(j *job.Job) {
	//isLogin,err := server.GetToken(j.GroupData["token"].(string))
	//if err == nil {
	//
	//}

	input := make(map[string]string)
	input["page"] = j.Input["page"]
	input["page_size"] = j.Input["page_size"]
	data := server.GoodsSearch(j.Input["title"], input)
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// GoodsInfo 商品详情
func GoodsInfo(j *job.Job) {
	if j.Input["goods_id"] == "" {
		j.JsonError(nil, "缺少参数")
		return
	}
	//isLogin, err := server.GetToken(j.GroupData["token"].(string))
	input := make(map[string]string)
	input["goods_id"] = j.Input["goods_id"]
	data, err := server.GoodsInfo(input)
	if err != nil {
		gogo_log.Write("商品详情", "server", err.Error())
		j.JsonError(nil, "商品不存在")
		return
	}

	j.JsonSuccess(data)
	return
}
